<template>
  <h1>员工列表综合练习</h1>
  <el-form style="width:600px;margin: 0 auto;">
    <el-form-item label="姓名：">
      <el-input placeholder="请输入姓名" v-model="emp.name"></el-input>
    </el-form-item>
    <el-form-item label="工资：">
      <el-input placeholder="请输入工资" v-model="emp.salary"></el-input>
    </el-form-item>
    <el-form-item label="岗位：">
      <el-input placeholder="请输入岗位" v-model="emp.job"></el-input>
    </el-form-item>
    <el-form-item>
      <el-button type="success" @click="add">提交</el-button>
    </el-form-item>
  </el-form>

  <el-table :data="arr" style="width: 600px;margin: 0 auto;">
    <el-table-column label="编号" type="index" width="80" align="center"></el-table-column>
    <el-table-column label="姓名" prop="name"></el-table-column>
    <el-table-column label="工资" prop="salary"></el-table-column>
    <el-table-column label="岗位" prop="job"></el-table-column>
    <el-table-column label="操作">
      <template #default="scope">
        <el-button type="danger" @click="del(scope.$index,scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script setup>
import {ref} from "vue";
import {ElMessage} from "element-plus";

//1.定义对象用作双向绑定收集员工数据
const emp = ref({name:'',salary:'',job:''})
//2.定义数组用来保存多个员工对象
const arr = ref([]);
//3.定义添加员工的方法
const add = () =>{
  if (emp.value.name.trim()==''||emp.value.salary.trim()==''||emp.value.job.trim()==''){
    ElMessage.error('员工信息不能为空')
  }
  //5.将收集到的员工数据存入数组中
  arr.value.push(emp.value);
  //6.将每次存入数组后，将emp对象重置为空对象
  emp.value={name:'',salary: '',job:''};
  console.log(arr.value);
}
//7.定义删除方法
const del = (index,emp)=>{
  if (confirm('你确定要删除'+emp.name+'吗？')){
    arr.value.splice(index,1);
    ElMessage.success('删除成功');
  }
}
</script>

<style scoped>

</style>